package com.yhy.wxzn.wcs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yhy.wxzn.common.enums.WcsBindStatusEnums;
import com.yhy.wxzn.common.exception.WmsBusinessException;
import com.yhy.wxzn.wcs.entity.WcsContainerBind;
import com.yhy.wxzn.wcs.entity.WcsDeviceInfo;
import com.yhy.wxzn.wcs.mapper.WcsContainerBindMapper;
import com.yhy.wxzn.wcs.service.IWcsContainerBindService;
import com.yhy.wxzn.wcs.service.IWcsDeviceLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Description: 容器扫码表
 * @Author: jeecg-boot
 * @Date:   2023-10-28
 * @Version: V1.0
 */
@Service
public class WcsContainerBindServiceImpl extends ServiceImpl<WcsContainerBindMapper, WcsContainerBind> implements IWcsContainerBindService {

    @Autowired
    private IWcsDeviceLogService wcsDeviceLogService;

//    @Override
//    public void insertContainerBind(String deviceNo, String barcode) {
//        String content = deviceNo+"号读码器:读取内容为:【"+barcode+"】";
//        //增加扫码日志
//        wcsDeviceLogService.genBcrReadInfo(deviceNo,content);
//        //获取设备信息
//        BcrDriver bcr = BcrServer.bcrDriverMap.get(deviceNo);
//        WcsDeviceInfo deviceInfo  = bcr.getBcr();
//        int taskType = 0;
//        switch (taskType){
//            case 0:
//                //母托盘任务
//                genParentPalletTask(deviceInfo,barcode);
//                break;
//            case 1:
//                //子母托任务
//                genSubPalletTask(deviceInfo,barcode);
//                break;
//            case 2:
//            case 3:
//            case 4:
//            case 5:
//                genScanTask(deviceInfo,barcode);
//                break;
//            default:
//                throw new WmsBusinessException("未知任务类型");
//        }
//    }

    @Override
    public List<WcsContainerBind> getUnfinishedList(String source) {
        LambdaQueryWrapper<WcsContainerBind> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WcsContainerBind::getSource, source)
                .lt(WcsContainerBind::getStatus, WcsBindStatusEnums.FFB.getValue())
                .orderByAsc(WcsContainerBind::getCreateTime);
        return this.list(wrapper);
    }

    private void genParentPalletTask(WcsDeviceInfo deviceInfo, String barcode) {
        LambdaQueryWrapper<WcsContainerBind> bindQuery = new LambdaQueryWrapper<>();
        bindQuery.eq(WcsContainerBind::getSource,deviceInfo.getRemark());
        bindQuery.eq(WcsContainerBind::getStatus,WcsBindStatusEnums.FSB.getValue());
        bindQuery.orderByDesc(WcsContainerBind::getCreateTime);
        List<WcsContainerBind> binds = this.list(bindQuery);
        if(!CollectionUtils.isEmpty(binds)){
            WcsContainerBind bindData = binds.get(0);
            bindData.setTpCode(barcode);
            bindData.setSource(deviceInfo.getRemark());
            bindData.setStatus(WcsBindStatusEnums.SSB.getValue());
            this.saveOrUpdate(bindData);
        }
    }

    private void genSubPalletTask(WcsDeviceInfo deviceInfo, String barcode) {
        LambdaQueryWrapper<WcsContainerBind> bindQuery = new LambdaQueryWrapper<>();
        bindQuery.eq(WcsContainerBind::getSource,deviceInfo.getRemark());
        bindQuery.eq(WcsContainerBind::getStatus,WcsBindStatusEnums.FSB.getValue());
        List<WcsContainerBind> binds = this.list(bindQuery);
        if(!CollectionUtils.isEmpty(binds)){
            this.remove(bindQuery);
        }
        WcsContainerBind bindData = new WcsContainerBind();
        bindData.setPriority(5);
        bindData.setTsCode(barcode);
        bindData.setSource(deviceInfo.getRemark());
        bindData.setStatus(WcsBindStatusEnums.FSB.getValue());
        this.save(bindData);
    }

    private void genScanTask(WcsDeviceInfo deviceInfo, String barcode) {
        LambdaQueryWrapper<WcsContainerBind> bindQuery = new LambdaQueryWrapper<>();
        bindQuery.eq(WcsContainerBind::getSource,deviceInfo.getRemark());
        bindQuery.lt(WcsContainerBind::getStatus,WcsBindStatusEnums.FFB.getValue());
        List<WcsContainerBind> binds = this.list(bindQuery);
        if(!CollectionUtils.isEmpty(binds)){
            this.remove(bindQuery);
        }
        WcsContainerBind bindData = new WcsContainerBind();
        bindData.setPriority(5);
        bindData.setTpCode(barcode);
        bindData.setSource(deviceInfo.getRemark());
        bindData.setStatus(WcsBindStatusEnums.WJB.getValue());
        this.save(bindData);
    }
}
